package com.dbq.fast.common.net.mock;

import com.dbq.fast.common.net.HttpManager;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by sunkai on 2017/10/30.
 */

public class MockRouter {

    private static Map<String, MockInterface> mockRouterMap = new HashMap<>();

    public static final String BASE_URL = HttpManager.HOST;


    static {
//        mockRouterMap.put(MockLogin.URL, new MockLogin());
//        mockRouterMap.put(MockVPNConfig.URL_GET_VPN, new MockVPNConfig());
//        mockRouterMap.put(MockVPNConfig.URL_UPLOAD_VPN_STATE, new MockVPNConfig());
//        mockRouterMap.put(MockPacakge.URL_GET_SERVICE, new MockPacakge());
//        mockRouterMap.put(MockPacakge.URL_GET_PACKAGE, new MockPacakge());
//        mockRouterMap.put(MockPacakge.URL_BUY_PACKAGE, new MockPacakge());
//        mockRouterMap.put(MockOrder.URL_GET_ORDER_LIST, new MockOrder());
//        mockRouterMap.put(MockLogOut.URL_LOGOUT, new MockLogOut());
//        mockRouterMap.put(MockRefreshToken.URL_REFRESH_TOKEN, new MockRefreshToken());
//        mockRouterMap.put(MockAccessWbProfile.URL, new MockAccessWbProfile());
//        mockRouterMap.put(MockSubscriptionMeal.URL, new MockSubscriptionMeal());
//        mockRouterMap.put(MockQueryActiveMoney.URL, new MockQueryActiveMoney());
//        mockRouterMap.put(MockQueryMyReward.URL, new MockQueryMyReward());
//        mockRouterMap.put(MockQueryWithdraw.URL, new MockQueryWithdraw());
//        mockRouterMap.put(MockBindWx.URL, new MockBindWx());

        mockRouterMap = Collections.unmodifiableMap(mockRouterMap);
    }

    public static Response intercept(Interceptor.Chain chain) {
        Response response = null;
        Response.Builder responseBuilder = new Response.Builder()
                .code(200)
                .message("")
                .request(chain.request())
                .protocol(Protocol.HTTP_1_0)
                .addHeader("content-type", "application/json");
        Request request = chain.request();
        String url = request.url().toString();
        if (url.contains("?")) {
            url = url.substring(0, url.indexOf("?"));
        }
        if (mockRouterMap.keySet().contains(url)) { //拦截指定地址
            responseBuilder.body(ResponseBody.create(MediaType.parse("application/json"), mockRouterMap.get(url).getResponse(url)));//将数据设置到body中
            response = responseBuilder.build(); //builder模式构建response
        } else {
            try {
//                response = chain.proceed(request);
//                final String responseString = new String(response.body().bytes());
//                Log.e("SK", "request:" + chain.request().url().toString());
//                if ("POST".equals(request.method())) {
//                    Request copy = request.newBuilder().build();
//                    Buffer buffer = new Buffer();
//                    copy.body().writeTo(buffer);
//                    Log.e("SK", "RequestParams:" + buffer.readUtf8().toString());
//
//                }
//
//                Log.e("SK", "response:" + responseString);
////
//                return response.newBuilder()
//                        .body(ResponseBody.create(response.body().contentType(), responseString))
//                        .build();
                response = chain.proceed(request);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return response;
    }
}
